﻿using HookahProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace HookahProject.Controllers
{
    public class MixtureController : Controller
    {
        private HookahEntities2 _wn = new HookahEntities2();

        private IQueryable<Mixture> GetMixtures(string UserID)
        {
            return _wn.Mixture.Where(it => it.UserId == UserID);
        }

        private void DeleteMixture(Mixture obj)
        {
            Mixture del = GetMixture(obj.Id);
            _wn.DeleteObject(del);
            _wn.SaveChanges();
        }

        private Mixture GetMixture(int id)
        {
            return _wn.Mixture.SingleOrDefault(it => it.Id == id);
        }

        private void AddMixture(float MixtureRating, string Comments, string UserId, List<int> Tobacco)
        {
            var newMixture = new Mixture { MixtureRating = MixtureRating, Comments = Comments, UserId = UserId };
            _wn.Mixture.AddObject(newMixture);
            foreach (int TobaccoId in Tobacco)
            {
                var newTobaccoMixture = new TobaccoMixture { TobaccoId = TobaccoId, MixtureId = newMixture.Id };
                _wn.TobaccoMixture.AddObject(newTobaccoMixture);
            }

            _wn.SaveChanges();
        }

        private void EditMixture(Mixture obj)
        {
            Mixture old = GetMixture(obj.Id);
            old.MixtureRating = obj.MixtureRating;
            old.Comments = obj.Comments;
            _wn.SaveChanges();

        }

        public ActionResult Index()
        {
            MembershipUser myObject = Membership.GetUser();
            string UserID = myObject.ProviderUserKey.ToString();
            return View(GetMixtures(UserID));
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(MixtureViewModel MVM)
        {
            if (ModelState.IsValid)
            {
                MembershipUser myObject = Membership.GetUser();
                string UserID = myObject.ProviderUserKey.ToString();
                AddMixture(MVM.MixtureRating, MVM.Comments, UserID, MVM.TobaccoList.Select(list => list.Id).ToList());
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

       
        [HttpGet]
        public ActionResult Edit(int id)
        {
            return View(GetMixture(id));
        }

        [HttpPost]
        public ActionResult Edit(Mixture obj)
        {
            if (ModelState.IsValid)
            {
                EditMixture(obj);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

     
        [HttpPost]
        public ActionResult Delete(Mixture obj)
        {
           DeleteMixture(obj);
           return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
